class ManagerUsersController < AdminController
  def index
    @users = User.all

    respond_to do |format|
      format.html # index.html.erb
      format.json { render json: @users }
    end
  end

  def show
    @user = User.find(params[:id])

    respond_to do |format|
      format.html # show.html.erb
      format.json { render json: @user }
    end
  end

  def new
    @user = User.new
    @current_role = "USER"

    respond_to do |format|
      format.html # new.html.erb
      format.json { render json: @user }
    end
  end

  def edit
    @user = User.find(params[:id])
    if(@user.role == 1)
      @current_role = "ADMIN"
    else
      @current_role = "USER"
    end
  end

  def create
    @user = User.new(params[:user])
    if(params[:role_user].eql? "ADMIN")
    @user.role = 1
    else
    @user.role = 0
    end

    respond_to do |format|
      if @user.save
         #save history         
          user = session[:user]
          history = History.new      
          history.userid = user.id
          history.useremail = user.email
          history.userrole = user.role
          history.action= "Admin create new user: " +@user.fullname
          history.save
          
        format.html { redirect_to manager_user_url(@user), :method => :get, notice: 'User was successfully created.' }
        format.json { render json: @user, status: :created, location: @user }
      else
        format.html { render action: "new" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def update
    @user = User.find(params[:id])
    if(params[:role_user].eql? "ADMIN")
    @user.role = 1
    else
    @user.role = 0
    end

    respond_to do |format|
      if @user.update_attributes(params[:user])
        
        #save history         
          user = session[:user]
          history = History.new      
          history.userid = user.id
          history.useremail = user.email
          history.userrole = user.role
          history.action= "Admin update user" + @user.fullname
          history.save
        format.html { redirect_to manager_user_url(@user), :method => :get, notice: 'User was successfully updated.' }
        format.json { head :no_content }
      else
        format.html { render action: "edit" }
        format.json { render json: @user.errors, status: :unprocessable_entity }
      end
    end
  end

  def destroy
    @user = User.find(params[:id])
    
    #save history         
          user = session[:user]
          history = History.new      
          history.userid = user.id
          history.useremail= user.email
          history.userrole = user.role
          history.action= "Admin delete user id %d" % @user.id
          history.save
          
    @user.destroy
    
    respond_to do |format|
      format.html { redirect_to manager_users_url }
      format.json { head :no_content }
    end
  end

  def delete_multiple_users
    if(params[:user_ids] != nil)
      @users = User.find(params[:user_ids])
      @users.each do |user|
         user = session[:user]
          history = History.new      
          history.userid = user.id
          history.useremail = user.email
          history.userrole = user.role
          history.action= "Admin delete user id" + user.id
          history.save
        user.destroy
      end
    end

    respond_to do |format|
      format.html { redirect_to manager_users_url }
      format.json { head :no_content }
    end
  end

  def search_users
    @users = User.find(:all, :conditions => ["id LIKE ? AND fullname LIKE ? AND email LIKE ?", "%" + params[:search_iduser] + "%", "%" + params[:search_username] + "%", "%" + params[:search_email] + "%"])

    respond_to do |format|
      format.html
      format.json { head :no_content }
    end
  end
end
